using System;
using System.Collections.Generic;
using TinaX;
using TinaX.UIKit;
using UnityEngine;
using UnityEngine.UI;

namespace Nekonya.UI
{
    public class MessageBox : XUIBehaviour
    {
        public Text txt_title; //标题（依赖注入
        public Text txt_content; //正文内容（依赖注入
        public Button btn_close; //右上角关闭按钮（依赖注入
        public GameObject go_ButtonItem; //大按钮 模板 （依赖注入
        public GameObject go_btns_content; //存放大按钮们的容器（依赖注入

        private OpenMessageBoxParams m_OpenUIArgs; //UI启动参数


        public class OpenMessageBoxParams
        {
            public string Title { get; set; }
            public string Content { get; set; }
            public bool ShowCloseButton { get; set; }

            public bool CloseByKeyboard { get; set; } = true;
            /// <summary>
            /// 可通过手柄关闭
            /// </summary>
            public bool CloseByHandle { get; set; } = true;

            public IList<MsgBoxButton> Buttons { get; set; }

            /// <summary>
            /// 通过右上角按钮关闭，键盘关闭和手柄关闭，都会尝试触发这个
            /// 点击空白处关闭的话不会触发。
            /// </summary>
            public Action OnCancel { get; set; }
        }

        public class MsgBoxButton
        {
            /// <summary>
            /// 按钮标题
            /// </summary>
            public string Title { get; set; }
            public BtnStyle Style { get; set; } = BtnStyle.Normal;
            public Action OnClick { get; set; }

            /// <summary>
            /// 当点击一次该按钮后，随即关闭弹窗
            /// 默认为true
            /// </summary>
            public bool Close { get; set; } = true;
        }

        /// <summary>
        /// 消息框 按钮风格
        /// </summary>
        public enum BtnStyle
        {
            /// <summary>
            /// 普通按钮
            /// </summary>
            Normal = 0,

            /// <summary>
            /// 强调按钮（预定为蓝色
            /// </summary>
            Emphasize = 1,

            /// <summary>
            /// 安全色按钮
            /// </summary>
            Safe = 2,

            /// <summary>
            /// 警告色按钮
            /// </summary>
            Warning = 3,
        }

        public override void Awake()
        {
            //清理掉UI上一些默认的东西（为了方便编辑，UI上肯定得有些东西的, 不应该给用户展示这些
            txt_title.text = string.Empty;
            txt_content.text = string.Empty;
            btn_close.gameObject.Hide();
            go_ButtonItem.Hide();
        }

        public override void OnMessage(string msgName, params object[] msgParams)
        {
            if (msgName.Equals(UIEventConst.OpenUI))
            {
                this.OnOpenUIMessage((OpenMessageBoxParams)msgParams[0]);
            }
        }

        private void OnOpenUIMessage(OpenMessageBoxParams _param)
        {
            m_OpenUIArgs = _param;
        }

        public override void Start()
        {
            if (m_OpenUIArgs == null) return;
            //开始刷界面
            txt_title.text = m_OpenUIArgs.Title;
            txt_content.text = m_OpenUIArgs.Content;

            //关闭按钮
            if (m_OpenUIArgs.ShowCloseButton)
            {
                btn_close.gameObject.Show();
                btn_close.onClick.RemoveAllListeners();
                btn_close.onClick.AddListener(() =>
                {
                    m_OpenUIArgs.OnCancel?.Invoke();
                    this.Close();
                });
            }

            //刷按钮
            if (m_OpenUIArgs.Buttons != null && m_OpenUIArgs.Buttons.Count > 0)
            {
                foreach (var btnInfo in m_OpenUIArgs.Buttons)
                {
                    var go_btn = GameObject.Instantiate(go_ButtonItem, go_btns_content.transform)
                        .Show();
                    var btn = go_btn.GetComponent<Button>();
                    var txt_btn = go_btn.transform.Find("Text").GetComponent<Text>();

                    txt_btn.text = btnInfo.Title;

                    bool closeByThisBtn = btnInfo.Close;
                    var onclick_action = btnInfo.OnClick;
                    btn.onClick.RemoveAllListeners();
                    btn.onClick.AddListener(() =>
                    {
                        onclick_action?.Invoke();
                        if (closeByThisBtn)
                        {
                            this.Close();
                        }
                    });

                    //按钮主题
                    //Todo : 按钮主题的美术素材暂时没有，得等台式机就绪了之后搞
                }
            }

            //按键关闭和手柄关闭
            //var input = new GameInputActions();
            //input.Common.Cancel.performed += context =>
            //{
            //    if (this.m_OpenUIArgs != null)
            //    {
            //        this.m_OpenUIArgs.OnCancel?.Invoke();
            //        this.Close();
            //    }
            //};
            //input.Common.Cancel.Enable();
            //m_DisposableGroup.Register(input);
        }


        public override void OnDestroy()
        {
            //m_DisposableGroup?.Dispose();
        }
    }
}
